package com.chenfan.magic.utils;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.Random;

/**
 * 随机工具类
 *
 * @author troy
 * @email 1747128171@qq.com
 * @date 18:49 2019/12/1 0001
 */
public class RandomKit {
    private static char[] chars = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e',
            'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z'};

    private static char[] nubers = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};

    /**
     * 生成随机字符串，包含数字和字母
     *
     * @param length 长度
     * @return
     */
    public static String randomStr(int length) {
        return RandomStringUtils.random(length, chars);
    }

    /**
     * 生成随机字符串，只包含数字
     *
     * @param length 长度
     * @return
     */
    public static String randomInt(int length) {
        return RandomStringUtils.random(length, nubers);
    }

    /**
     * 随机生成指定范围的随机数, 如 5~26, 包含 26
     *
     * @param start 起始
     * @param end   结束
     * @return
     */
    public static int randomInt(int start, int end) {
        if (start > end) {
            int tmp = end;
            end = start;
            start = tmp;
        }
        Random rand = new Random();
        return rand.nextInt((end - start) + 1) + (start);
    }

    public static void main(String[] args) {
        System.out.println(randomStr(4));
        System.out.println(randomStr(6));
        System.out.println(randomInt(5, 26));
    }
}
